package com.mdd.admin.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.service.IGreetingCardsRecordService;
import com.mdd.admin.validate.GreetingCardsRecordCreateValidate;
import com.mdd.admin.validate.GreetingCardsRecordUpdateValidate;
import com.mdd.admin.validate.GreetingCardsRecordSearchValidate;
import com.mdd.admin.vo.GreetingCardsRecordListedVo;
import com.mdd.admin.vo.GreetingCardsRecordDetailVo;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.GreetingCardsRecord;
import com.mdd.common.mapper.GreetingCardsRecordMapper;
import com.mdd.common.util.TimeUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import javax.annotation.Resource;
import java.util.*;

/**
 * 赠送贺卡记录实现类
 * @author DEREK
 */
@Service
public class GreetingCardsRecordServiceImpl implements IGreetingCardsRecordService {
        
    @Resource
    GreetingCardsRecordMapper greetingCardsRecordMapper;

    /**
     * 赠送贺卡记录列表
     *
     * @author DEREK
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<GreetingCardsRecordListedVo>
     */
    @Override
    public PageResult<GreetingCardsRecordListedVo> list(PageValidate pageValidate, GreetingCardsRecordSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<GreetingCardsRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        greetingCardsRecordMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:type:long",
            "=:cardId@card_id:long",
            "=:userId@user_id:long",
            "=:toUserId@to_user_id:long",
            "=:content:str",
            "=:remark:str",
        });

        IPage<GreetingCardsRecord> iPage = greetingCardsRecordMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<GreetingCardsRecordListedVo> list = new LinkedList<>();
        for(GreetingCardsRecord item : iPage.getRecords()) {
            GreetingCardsRecordListedVo vo = new GreetingCardsRecordListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(TimeUtils.timestampToDate(item.getCreateTime()));
            vo.setUpdateTime(TimeUtils.timestampToDate(item.getUpdateTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 赠送贺卡记录详情
     *
     * @author DEREK
     * @param id 主键参数
     * @return GreetingCardsRecord
     */
    @Override
    public GreetingCardsRecordDetailVo detail(Integer id) {
        GreetingCardsRecord model = greetingCardsRecordMapper.selectOne(
                new QueryWrapper<GreetingCardsRecord>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        GreetingCardsRecordDetailVo vo = new GreetingCardsRecordDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 赠送贺卡记录新增
     *
     * @author DEREK
     * @param createValidate 参数
     */
    @Override
    public void add(GreetingCardsRecordCreateValidate createValidate) {
        GreetingCardsRecord model = new GreetingCardsRecord();
        model.setType(createValidate.getType());
        model.setCardId(createValidate.getCardId());
        model.setUserId(createValidate.getUserId());
        model.setToUserId(createValidate.getToUserId());
        model.setContent(createValidate.getContent());
        model.setCreateTime(System.currentTimeMillis() / 1000);
        model.setRemark(createValidate.getRemark());
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        greetingCardsRecordMapper.insert(model);
    }

    /**
     * 赠送贺卡记录编辑
     *
     * @author DEREK
     * @param updateValidate 参数
     */
    @Override
    public void edit(GreetingCardsRecordUpdateValidate updateValidate) {
        GreetingCardsRecord model = greetingCardsRecordMapper.selectOne(
                new QueryWrapper<GreetingCardsRecord>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setType(updateValidate.getType());
        model.setCardId(updateValidate.getCardId());
        model.setUserId(updateValidate.getUserId());
        model.setToUserId(updateValidate.getToUserId());
        model.setContent(updateValidate.getContent());
        model.setRemark(updateValidate.getRemark());
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        greetingCardsRecordMapper.updateById(model);
    }

    /**
     * 赠送贺卡记录删除
     *
     * @author DEREK
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        GreetingCardsRecord model = greetingCardsRecordMapper.selectOne(
                new QueryWrapper<GreetingCardsRecord>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        greetingCardsRecordMapper.delete(new QueryWrapper<GreetingCardsRecord>().eq("id", id));
    }

}
